<?php
/**
 * @file forumreplayaction.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2015-9-24 下午4:14:32 $
 * @brief  $
 */
namespace haibao\cms\business;

class ForumReplayAction extends \haibao\cms\business\Base{
	
	/**
	 * @var $_instance
	 */
	private static $_instance;
	
	/**
	 * instance()
	 */
	private static function instance(){
		if(!self::$_instance){
			self::$_instance = new \haibao\cms\data\ForumReplayAction();
		}
		return self::$_instance;
	}
	
	/**
	 * 获取帖子回复列表
	 */
	public static function getReplayList($model){
		return self::instance()->getAll($model);
	}
	
	/**
	 * 获取每条帖子已回复数
	 */
	public static function getRestCount($forumReplayIdArr){
		return self::instance()->getRestCount($forumReplayIdArr);
	}
	
	/**
	 * 关闭帖子回复
	 */
	public static function closeForumReplay($forumId){
		self::checkOperation(self::FUNC_CODE_COMMUNITY_FORUM_LIST);
		self::checkCloseForm($forumId);
		$forumIdStr = str_replace("，",",",$forumId);
		$forumIdArr = explode(',',$forumIdStr);//处理
		if($forumIdArr){
			$forumIdArr = array_filter($forumIdArr);
		}
		self::instance()->closeForumReplay($forumIdArr);
		self::saveOperateHistory(self::FUNC_CODE_COMMUNITY_FORUM_LIST,$forumId);
	}
	
	/**
	 * 删除帖子回复
	 */
	public static function deleteReplay($delForumId,$forumFloorOne,$forumFloorTwo){
		self::checkOperation(self::FUNC_CODE_COMMUNITY_DELETE_FORUM_LIST);
		
		if($delForumId == ''){
			throw new BusinessException('帖子ID不能为空');
		}
		if($forumFloorOne == ''){
			throw new BusinessException('起始楼层数不能为空');
		}
		
		if($forumFloorTwo == ''){
			throw new BusinessException('结束楼层数不能为空');
		}
		
		if($forumFloorOne > $forumFloorTwo){
			throw new BusinessException('起始楼层不能大于结束楼层');
		}
		
		self::instance()->deleteReplay($delForumId,$forumFloorOne,$forumFloorTwo);
		self::saveOperateHistory(self::FUNC_CODE_COMMUNITY_DELETE_FORUM_LIST,array($forumFloorOne,$forumFloorTwo));
	}
	
	/**
	 * 添加批量回复帖子操作
	 * @param unknown $model
	 */
	public static function saveForumReplayAction($model){
		self::checkOperation(self::FUNC_CODE_COMMUNITY_FORUM_REPLAY);
		self::checkForm($model);
		$model->UserId = self::getCurrentUser()->Id;
		$model->CreateTime = new \DateTime();
		self::instance()->saveForumReplayAction($model);
		self::saveOperateHistory(self::FUNC_CODE_COMMUNITY_FORUM_REPLAY, $model->toArray(true,true));
	}
	
	/**
	 * 修改帖子浏览数
	 */
	public static function editViewNum($model){
		self::checkOperation(self::FUNC_CODE_COMMUNITY_FORUM_REPLAY);
		self::checkViem($model);
		self::instance()->editViewNum($model);
		self::saveOperateHistory(self::FUNC_CODE_COMMUNITY_FORUM_REPLAY, $model->toArray(true,true));
	}
	
	private static function checkCloseForm($forumId){
		if($forumId == ''){
			throw new BusinessException('帖子ID不能为空');
		}

		$checkStr = '/^[，,0-9]*$/';
		if(!preg_match($checkStr, $forumId, $match)){
			throw new BusinessException('帖子ID有特殊字符，请重新填写');
		}
		
	}
	
	private static function checkViem($model){
		if(isset($model->ForumId) && $model->ForumId == ''){
			throw new BusinessException('帖子ID不能为空');
		}
		
		if(isset($model->ViewNum) && $model->ViewNum == ''){
			throw new BusinessException('帖子浏览数不能为空');
		}
		
		if(isset($model->ForumId)){
			$result = self::instance()->checkForumId($model->ForumId);
			if(empty($result)){
				throw new BusinessException('帖子ID不存在');
			}
		}
	}
	
	/**
	 * 表单验证
	 * @param \haibao\app\model\data\Article $model
	 */
	private static function checkForm($model){
		if(isset($model->ForumId) && $model->ForumId == ''){
			throw new BusinessException('帖子ID不能为空');
		}
		
		if(isset($model->ForumId)){
			$result = self::instance()->checkForumId($model->ForumId);
			if(empty($result)){
				throw new BusinessException('帖子ID不存在');
			}
		}
		
		if($model->ForumId){
			$checkStr = '/^[0-9]*$/';
			if(!preg_match($checkStr, $model->ForumId, $match)){
				throw new BusinessException('帖子ID有特殊字符，请重新填写');
			}
		}
		
		if($model->StartDate == '' || $model->EndDate == ''){
			throw new BusinessException('日期范围不能为空');
		}
	
		if($model->StartDate > $model->EndDate){
			throw new BusinessException('开始日期不能大于结束日期');
		}
		
		if(isset($model->StartTime) && $model->StartTime!=''){
			$checkStr = '/^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/';
			if(!preg_match($checkStr, $model->StartTime, $match)){
				throw new BusinessException('开始时间格式不正确，请重新填写');
			}
		}
		
		if(isset($model->EndTime) && $model->EndTime!=''){
			$checkStr = '/^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/';
			if(!preg_match($checkStr, $model->EndTime, $match)){
				throw new BusinessException('结束时间格式不正确，请重新填写');
			}
		}
		
		if($model->StartTime == ''){
			$model->StartTime = '00:00:00';
		}
		
		if($model->EndTime == ''){
			$model->EndTime = '23:59:59';
		}
		
		if($model->StartTime > $model->EndTime){
			throw new BusinessException('开始时间不能大于结束时间');
		}
	
		if($model->Count == ''){
			throw new BusinessException('数量不能为空');
		}
	
		if($model->Count){
			$checkStr = '/^\d*$/';
			if(!preg_match($checkStr, $model->Count, $match)){
				throw new BusinessException('数量有特殊字符，请重新填写');
			}
		}
		
		if($model->Content == ''){
			throw new BusinessException('内容不能为空');
		}
		
	}
}